<?php
require_once (dirname(__FILE__) . "/../../config.php");
require_once (dirname(__FILE__) . "/HostDao.php");
require_once (dirname(__FILE__) . "/../common/crypt/Crypt.php");
?>
<?php

/*
 * Server DB
 * Created: 200904 (TT Tsai)
 *
 *
 */
class WFServerDao extends WFHostDao
{
    // my error handling
    private $errMessage = "";
    // Constructor
    public function __construct($dbconfig, $uid){
        parent::__construct($dbconfig["table"]["serverlist"]["name"], $dbconfig, $uid);
    }
    // add
    public function add($sname, $sinfo, $arrDefaultPasswords){
        // password encryption
        if(isset($sinfo['customFields']['password']) && is_array($sinfo['customFields']['password'])){
            foreach($sinfo['customFields']['password'] as $key => $item){
                if(isset($item)){
                    $sinfo['customFields']['password'][$key] = parent::encrypt($item);
                    if(parent::isFailed()){
                        $this->errMessage = '[serverinfo] custom fields encryption failed:' . parent::getErrMessage();
                        return;
                    }
                }else{
                    if(isset($arrDefaultPasswords) && is_array($arrDefaultPasswords)){
                        foreach($arrDefaultPasswords as $arrDefaultPasswordName => $arrDefaultPassword){
                            if($arrDefaultPasswordName === $key){
                                $sinfo['customFields']['password'][$key] = $arrDefaultPassword;
                            }
                        }
                    }
                }
            }
        }
        $enc_str = parent::encrypt($sinfo["password"]);
        if(parent::isFailed()){
            $this->errMessage = "[serverinfo] encryption module failed(encrypt): " . parent::getErrMessage();
            return;
        }
        $sinfo["password"] = $enc_str;
        parent::add($sname, json_encode($sinfo));
    }
    public function add_str($sname, $sinfo){
        // conver to json
        $sinfo_json = json_decode($sinfo, true);
        if($sinfo_json == null){
            $this->errMessage = "[serverinfo] incorrect format";
            return;
        }
        $this->add($sname, $sinfo_json);
    }
    // del
    public function del($rid){
        // data validation
        if(! is_numeric($rid)){
            $this->errMessage = "[serverid] should be a number";
            return;
        }
        parent::del($rid);
    }
    // update
    public function update($rid, $sname, $sinfo, $arrDefaultPasswords = null){
        // data validation
        if(! is_numeric($rid)){
            $this->errMessage = "[serverid] should be a number";
            return;
        }
        if($rid !== "1"){
            // password encryption
            if(isset($sinfo['customFields']['password']) && is_array($sinfo['customFields']['password'])){
                $query_result = parent::getOriginalInfo($rid);
                foreach($sinfo['customFields']['password'] as $key => $item){
                    if(isset($item)){
                        $sinfo['customFields']['password'][$key] = parent::encrypt($item);
                        if(parent::isFailed()){
                            $this->errMessage = '[serverinfo] custom fields encryption failed:' . parent::getErrMessage();
                            return;
                        }
                    }else{
                        $useDefaultPassword = false;
                        if(isset($arrDefaultPasswords) && is_array($arrDefaultPasswords)){
                            foreach($arrDefaultPasswords as $arrDefaultPasswordName => $arrDefaultPassword){
                                if($arrDefaultPasswordName === $key){
                                    $sinfo['customFields']['password'][$key] = $arrDefaultPassword;
                                    $useDefaultPassword = true;
                                }
                            }
                        }
                        if(! $useDefaultPassword && $query_result != FALSE && isset($query_result[0])){
                            $jsonobj = json_decode($query_result[0]["hostinfo"], true);
                            if($jsonobj != null){
                                if(isset($jsonobj['customFields']) && isset($jsonobj['customFields']['password']) && isset($jsonobj['customFields']['password'][$key])){
                                    $sinfo['customFields']['password'][$key] = $jsonobj['customFields']['password'][$key];
                                }else{
                                    $sinfo['customFields']['password'][$key] = '';
                                }
                            }else{
                                $sinfo['customFields']['password'][$key] = '';
                            }
                        }
                    }
                }
            }
            if(isset($sinfo['password'])){
                $enc_str = parent::encrypt($sinfo["password"]);
                if(parent::isFailed()){
                    $this->errMessage = "[serverinfo] encryption module failed(encrypt): " . parent::getErrMessage();
                    return;
                }
                $sinfo["password"] = $enc_str;
            }else{
                $query_result = parent::getOriginalInfo($rid);
                if($query_result != FALSE && isset($query_result[0])){
                    $jsonobj = json_decode($query_result[0]["hostinfo"], true);
                    if($jsonobj != null){
                        $sinfo['password'] = $jsonobj["password"];
                    }else{
                        $sinfo['password'] = '';
                    }
                }
            }
        }
        parent::update($rid, $sname, json_encode($sinfo));
    }
    public function update_str($rid, $sname, $sinfo){
        // conver to json
        $sinfo_json = json_decode($sinfo, true);
        if($sinfo_json == null){
            $this->errMessage = "[serverinfo] incorrect format";
            return;
        }
        $this->update($rid, $sname, $sinfo_json);
    }
    // getall
    public function getall(){
        return parent::getall();
    }
    // getinfo
    public function getinfo($rid){
        return parent::getinfo($rid);
    }
    // getinfoByGuid
    public function getinfoByGuid($guid){
        return parent::getinfoByGuid($guid);
    }
    // isFailed
    public function isFailed(){
        if($this->errMessage != ""){
            return TRUE;
        }
        return parent::isFailed();
    }
    //
    public function getErrMessage(){
        if($this->errMessage != ""){
            return $this->errMessage;
        }
        return parent::getErrMessage();
    }
}
?>
